﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblTerms
    /// </summary>
    public class TblTermsManagerBase
    {
        public TblTermsManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public int InsertUpdate(TblTermsEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@Output", item.Id);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@Name", item.Name));
            ls.Add(new SqlParameter("@Slug", item.Slug));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblTerm", sqlparam);
            return int.Parse(sqlparam[0].Value.ToString());
        }
        public int InsertUpdate(int Id, string Name, string Slug)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@Output", Id);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@Name", Name));
            ls.Add(new SqlParameter("@Slug", Slug));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblTerm", sqlparam);
            return int.Parse(sqlparam[0].Value.ToString());
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblTerm", sqlparam);
        }
        public TblTermsEntity GetTblTermsEntity(DataRow dr)
        {
            TblTermsEntity item = new TblTermsEntity();
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();
            if (dr.Table.Columns.Contains("Name"))
                item.Name = dr["Name"].ToString();
            if (dr.Table.Columns.Contains("Slug"))
                item.Slug = dr["Slug"].ToString();
            return item;
        }
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTerm", sqlparam);
        }
        public TblTermsEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTerm", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblTermsEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermsAll", null);
        }
        public List<TblTermsEntity> SelectAll()
        {
            List<TblTermsEntity> lsresult = new List<TblTermsEntity>();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblTermsEntity(dt.Rows[i]));
            return lsresult;
        }
    }
}

